feat(types): add inference for embeded joins by functions#614
feat(types): add inference for embeded joins by functions#614avallete wants to merge 15 commits intochore/add-auto-types-gen-and-override-for-testingfrom
Conversation
src/PostgrestClient.ts
Outdated
| // if rpc is called with a typeless client, default to infering everything as any | ||
| ): IsAny<Fn> extends true | ||
| ? PostgrestFilterBuilder< | ||
| Schema, | ||
| Fn['Returns'] extends any[] | ||
| ? Fn['Returns'][number] extends Record<string, unknown> | ||
| ? Fn['Returns'][number] | ||
| : never | ||
| : Fn['Returns'] extends Record<string, unknown> | ||
| ? Fn['Returns'] | ||
| : never, | ||
| Fn['Returns'], | ||
| FnName, | ||
| null | ||
| > | ||
| : PostgrestFilterBuilder< | ||
| // otherwise, provide the right params for typed .select chaining | ||
| Schema, | ||
| Fn['Returns'] extends any[] | ||
| ? Fn['Returns'][number] extends Record<string, unknown> | ||
| ? Fn['Returns'][number] | ||
| : never | ||
| : Fn['Returns'] extends Record<string, unknown> | ||
| ? Fn['Returns'] | ||
| : never, | ||
| Fn['Returns'], | ||
| Fn['SetofOptions'] extends GenericSetofOption ? Fn['SetofOptions']['to'] : FnName, | ||
| Fn['SetofOptions'] extends GenericSetofOption | ||
| ? Fn['SetofOptions']['to'] extends keyof Schema['Tables'] | ||
| ? Schema['Tables'][Fn['SetofOptions']['to']]['Relationships'] | ||
| : Schema['Views'][Fn['SetofOptions']['to']]['Relationships'] | ||
| : null | ||
| > { |
There was a problem hiding this comment.
note
This part should fix: supabase/supabase-js#1366
Now that we're able to detect embed from function within select, we can also use the same types to do the opposite and make:
.rpc('function_that_return_setof_table_A', {}).select('field_from_table_A')
And infer the right result.
| >( | ||
| columns?: Query | ||
| ): PostgrestTransformBuilder<Schema, Row, NewResultOne[], RelationName, Relationships> { | ||
| ): PostgrestFilterBuilder<Schema, Row, NewResultOne[], RelationName, Relationships> { |
There was a problem hiding this comment.
note
This should fix: supabase/supabase-js#1365
| Functions: { | ||
| get_user_profile_non_nullable: { | ||
| SetofOptions: { | ||
| isNotNullable: true |
There was a problem hiding this comment.
note
This will be the api to make any row returning function globally "non nullable" if desired by the user, to avoid having to use the ! operator everytime.
|
Is there any progress on this? |
|
Not much yet, it's on my backburner I need to find a fews days to get back on it. The main blocker was that late down the line I've found out a lot of corner cases with postgres functions that have some unnamed parameters which caused some issues. I need to untangle that first before going further with this. |
|
Closes in favor of: #632 |
What kind of change does this PR introduce?
Once supabase/postgres-meta#915 is merged
This introduce automatic types inferences for "joins based on functions" to
postgrest-js.Along with the type generation in
pg-metait should allows queries such as:To work out of the box.
It also introduce a way to "force cast" an object relation as being not nullable at the database override level in case some DB layer app logic enforce it.
This should not be a breaking change since everything has be scoped in an additional type.
Fixes: CLIBS-79
Related: supabase/supabase-js#1259
Edit: Tested the full pipeline (typegen + inference) over our infra repo, was able to get rid of all types override for the
billing_subscriptions(see: https://github.com/supabase/infrastructure/compare/develop...chore/upgrade-supabase-js)